package com.lz.sports.dialog

import android.os.Bundle
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.lz.library.dialog.BaseDialogFragment
import com.lz.library.extension.click
import com.lz.sports.R
import com.lz.sports.databinding.DialogEditPublicBinding
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class EditPublicDialog : BaseDialogFragment<DialogEditPublicBinding>() {

    companion object {
        fun show(activity: FragmentActivity, hint: String, title: String, content: String = "", requestCode: Int = -1): EditPublicDialog {
            val dialog = EditPublicDialog().also {
                it.arguments = Bundle().apply {
                    putString("title", title)
                    putString("hint", hint)
                    putString("content", content)
                    putInt("requestCode", requestCode)
                }
            }
            dialog.setFrom(true)
            dialog.show(activity.supportFragmentManager)
            return dialog
        }

        fun show(fragment: Fragment, hint: String, title: String, content: String = "", requestCode: Int = -1): EditPublicDialog {
            val dialog = EditPublicDialog().also {
                it.arguments = Bundle().apply {
                    putString("title", title)
                    putString("hint", hint)
                    putString("content", content)
                    putInt("requestCode", requestCode)
                }
            }
            dialog.show(fragment.childFragmentManager)
            return dialog
        }
    }

    override fun getLayoutRes(): Int = R.layout.dialog_edit_public

    override fun initData(v: View, savedState: Bundle?) {
        lifecycleScope.launch {
            arrayOf(binding.tvConfirm, binding.tvCancel).click().collect {
                when (it.id) {
                    R.id.tv_cancel -> dismiss()
                    R.id.tv_confirm -> {
                        val requestCode = arguments!!.getInt("requestCode")
                        val text = binding.etInput.text.toString().trim()
                        if (isFromActivity()) {
                            if (context is OnEditConfirmListener) {
                                (context as OnEditConfirmListener).onEditConfirm(this@EditPublicDialog, text, requestCode)
                            }
                        } else {
                            if (parentFragment is OnEditConfirmListener) {
                                (parentFragment as OnEditConfirmListener).onEditConfirm(this@EditPublicDialog, text, requestCode)
                            }
                        }
                    }
                }
            }
        }
        binding.tvTitle.text = arguments?.getString("title")
        binding.tvHint.text = arguments?.getString("hint")
        binding.etInput.setText(arguments?.getString("content"))

        binding.etInput.addTextChangedListener(object : TextWatcher {

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit

            override fun afterTextChanged(s: Editable?) {
                binding.tvConfirm.isEnabled = !TextUtils.isEmpty(s)
            }
        })
    }

    interface OnEditConfirmListener {

        fun onEditConfirm(dialog: EditPublicDialog, text: String, requestCode: Int)
    }
}